/*
 * Copyright 2021 yhuan416
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef _SBULL_INTERNAL_H_
#define _SBULL_INTERNAL_H_

#include "sbull.h"

#include "fs/driver.h"
#include "mtd_dev.h"
#include "mtd_partition.h"

// 擦除块大小=扇区大小 64K
#define SBULL_ERASE_SIZE    SBULL_SECTOR_SIZE

// mtdDev
void *GetMtd(const char *type);
int FreeMtd(struct MtdDev *mtd);

// Ops
struct block_operations *GetDevSpinorOps(void);
struct block_operations *GetDevNandOps(void);
struct file_operations_vfs *GetMtdCharFops(void);

// MTD device
int sbull_mtd_erase(struct MtdDev *mtd, UINT64 start, UINT64 bytes, UINT64 *failAddr);
int sbull_mtd_read(struct MtdDev *mtd, UINT64 start, UINT64 bytes, const char *buf);
int sbull_mtd_write(struct MtdDev *mtd, UINT64 start, UINT64 bytes, const char *buf);

// BLK device
int     sbull_blk_open(struct Vnode *vnode);
int     sbull_blk_close(struct Vnode *vnode);
ssize_t sbull_blk_read(struct Vnode *vnode, unsigned char *buffer, \
                            unsigned long long start_sector, unsigned int nsectors);
ssize_t sbull_blk_write(struct Vnode *vnode, const unsigned char *buffer, \
                            unsigned long long start_sector, unsigned int nsectors);
int     sbull_blk_geometry(struct Vnode *vnode, struct geometry *geometry);

// CHR device
int     sbull_char_open(struct file *filep);
int     sbull_char_close(struct file *filep);
ssize_t sbull_char_read(struct file *filep, char *buffer, size_t buflen);
ssize_t sbull_char_write(struct file *filep, const char *buffer, size_t buflen);
off_t   sbull_char_seek(struct file *filep, off_t offset, int whence);

#endif
